// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App";
import router from "./router";
import axios from "axios";

//Vant
import Vant from "vant";
import "vant/lib/vant-css/index.css";
Vue.use(Vant);

import "./config.js";
import { request } from "@/utils/request.js";
import { my } from "@/utils/my.js";

//复制
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

Vue.config.productionTip = false
Vue.prototype.request = request;
Vue.prototype.my = my;
Vue.prototype.axios = axios;

// 全局守卫
router.beforeEach((to, from, next) => {
  if(to.query['tid']){
    my.setLocal('tid',to.query['tid']);
  }
  let userinfo = my.getLocal("userinfo");
  Vue.prototype.userinfo = userinfo;
  let is_need_login = to.matched.some(record => {
      window.document.title = record.meta.title ? record.meta.title : window.my.title;
      return record.meta.requireAuth;
  });
  if (is_need_login) {
    if (!userinfo) return next('/login');
     //重新获取数据
     request("post", {
       url: "/user/user/getuserinfo",
       token: userinfo['token'],
       is_loading: true
     }).then(res => {
       if(res.code==200){
         my.removeLocal('userinfo');
         return next("/login");
       }else{
         Vue.prototype.userinfo = res.data.userinfo;
         my.setLocal('userinfo',res.data.userinfo);
         return next();
       }
     });
  }
  return next();
});

/* eslint-disable no-new */
new Vue({
  router,
  render: h => h(App)
}).$mount("#app");
